﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RBOSS.Business.AdminModule;
using RBOSS.Utilities;

namespace RBOSS.WebUI
{
    public partial class VendorRegister : System.Web.UI.Page
    {
        private Vendors vendor; 


        protected void Page_Load(object sender, EventArgs e)
        {
            vendor = new Vendors();
            if (!Page.IsPostBack)
            {
                
                Roles_sys us = new Roles_sys();
                ListItem lstItems;
                Dictionary<string, string> obj = us.getRoles();
                foreach (string item in obj.Keys)
                {
                    lstItems = new ListItem(obj[item], item);
                    RolesList1.Items.Add(lstItems);
                }

                RolesList1.SelectedIndex = -1;
            }
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            if (!vendor.CheckEmailAvailability(txtUserEmail.Text))
            {
                FailureText.Text = "Email address is already registered...";
                //ErrorMessage.Visible = true;
                return;
            }

            var userEmail = txtUserEmail.Text;
            var password = RBOSSecurity.Encrypt(txtPassword.Text);
            var firstName = txtFirstName.Text;
            var lastName = txtLastName.Text;

            var phoneNo = txtPhone.Text;
            var city = txtCity.Text;
            var provience = txtProvience.Text;
            var country = txtCountry.Text;
            var postalCode = txtPostalCode.Text;

            vendor.AddVendor(userEmail, password, firstName, lastName, phoneNo, city, provience, country, postalCode,int.Parse(RolesList1.SelectedValue));

            if (!RolesList1.Text.ToLower().Contains("internal"))
            {
                vendor.addBankDetails(userEmail, txtAccountNumber.Text, txtAccountType.Text, txtBankName.Text, txtBankBranch.Text);
            }
            FailureText.Text = "New Vendor Registered Successfully!!!";
            //ErrorMessage.Visible = true;
            ClearFields();
           // Response.Redirect("login.aspx");
        }

        private void ClearFields()
        {
            txtUserEmail.Text = string.Empty;
            txtPassword.Text = string.Empty;

            txtFirstName.Text = string.Empty;
            txtLastName.Text = string.Empty;

            txtPhone.Text = string.Empty;
            txtCity.Text = string.Empty;

            txtProvience.Text = string.Empty;
            txtCountry.Text = string.Empty;

            txtPostalCode.Text = string.Empty;
            txtAccountNumber.Text = ""; txtAccountType.Text = ""; txtBankName.Text = ""; txtBankBranch.Text = "";
        }
    }
}